Skip to content

nvn/window: give an acquired image back once the next present has run (DM_NVN_ACQ_NEXT) - #3372

Closed
willv678 wants to merge 1 commit into
seedds:mainfrom
willv678:pr/acq-next
Closed

willv678 wants to merge 1 commit into
seedds:mainfrom
willv678:pr/acq-next

Conversation

@willv678

Copy link
Copy Markdown
Contributor

What and why

On Horizon, a window image comes back to the game once the compositor has taken the next present, so the GPU has finished the frame after that image's last one. The host gave it back one frame earlier, once its own present's copy had run, and the fence the acquire waited on is the replay's newest, which can trail what the game has submitted.

Most games don't notice. Xenoblade Chronicles DE (Switch 2 Edition) does: two images, it waits only on the acquire sync (every nvnSyncWait in the intro is on it, infinite timeout), and it keeps its per-frame constants in two slots that alternate by frame. It wrote frame N+1's constants into the slot frame N-1 was still drawing with. Its 1x1 exposure texture went NaN and the tone mapping multiplied the whole picture by it: flat blue/purple frames with only the subtitles on top, plus streaks. Xenoblade 3's world streaks (#3296) have the same shape and might be the same thing.

The fix keeps each image's present order. nvnWindowAcquireTexture takes a reference to the ticket of the present after the acquired image's last one, and the acquire's wait (in nvnSyncWait, at the next acquire, or immediately without a sync) waits until that present has been replayed and the GPU is past the fence it ended at. With two images that's the frame before; with three, the one before that, same as Horizon. DM_NVN_ACQ_NEXT=0 turns it off.

About #1847: that PR moves the acquire sync the other way for DK Bananza (to the image's own release, because fencing it on every submission made DK wait on the frame it just submitted). The two don't conflict on the main point, both stop waiting on the newest submission, but #1847's fence is the image's own present, which is one frame too early for a game like Xenoblade. I ran DK on this branch to check it doesn't cost DK anything (below). Happy to rebase on whichever lands first, or talk it through on #1847.

Checked

  • Tests: t_nvn section 6a (new): with two images, the image presented before a present the queue holds behind an unsignaled event comes back next, its acquire's nvnSyncWait times out while the event is unsignaled and is signaled once the held present has run. It fails with DM_NVN_ACQ_NEXT=0. t_nvn 541/541, t_perf_order, t_reorder_chain, t_feedback, t_dlss_scaler pass.
  • Devices and OS: MacBook Pro M2 Pro, 32 GB, macOS 27.3 beta, docked, MetalFX on.
  • Xenoblade Chronicles DE Switch 2 Edition (0400FF500E34A001, 2.0.0, local profile, there's none in the repo yet): the intro had flat frames in 35 of 46 sampled frames before, none after. DM_NVN_MAGSCAN found NaN in 553 frames of a 150 s run before, 0 after.
  • Donkey Kong Bananza (040074A01BF12000, v458752) on main vs this branch, the same scripted run from a new save through the intro into the mining tunnel (about 7 minutes each): identical frame times in every 300-frame window (60 fps intro, 30 at the first punch prompt, 20 fps in the tunnel with the GPU at about 43 ms on this M2 Pro, which is the same on main), no new red lines, and the tunnel draws correctly.
  • Cost: a game that only waits on the frame before loses CPU/GPU overlap it never had on the console. Xenoblade's intro went from 17-18 ms (with the race) to about 21 ms. Other changes since got it back under 16.7.
  • regress.py: DK has no smoke route on main (games: boot smoke routes for Donkey Kong Bananza, Welcome Tour and Xenoblade Chronicles 3 #3189's needs the bench save), so I ran it by hand as above. Xenoblade has no profile on main.

Docs

  • docs/reference/frame-rate.md: "An acquired image comes back once the present after it has run"; docs/env_switches.tsv row; symptom index regenerated
  • No game files, keys or data extracted from a game in the change or the attachments

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 26, 2026
@FantasyGmm

Copy link
Copy Markdown

This looks fixed for my XC3 Colony 9 repro on Mac. I manually played XC3 2.2.1 with #3372 enabled in a normal window, without scripted input or capture options, and didn't see the corruption recur.

I also tested PR head a1b53cffc8f5 locally merged onto f56fddeb94db on an M4 Mac (docked, stock resolution and DRS, MetalFX off), using the same save and input sequence:

  • Clean main: the 3D world went flat green while the HUD stayed visible.
  • Merged build, default settings: no visible blanking or streaks in either of two runs (232 captured frames total).
  • Same merged binary, DM_NVN_ACQ_NEXT=0: the second of two runs reproduced the blue and near-black frames with the HUD still visible.

Full repro, screenshots, and pass dump in #3296.

@FantasyGmm

Copy link
Copy Markdown

Quick XC2 follow-up: I briefly played XC2 3.0.0 from the Central Exchange save on the same M4 Mac after locally merging a1b53cffc8f5 onto f56fddeb94db. I used a normal window with the fix enabled, stock resolution and DRS, MetalFX off, and no scripted input or capture. I didn't see the intermittent world blanking during this short check. Related: #3346.

seedds added a commit that referenced this pull request Nov 26, 2026
On Horizon, an acquired swapchain image is returned once the compositor has replaced it with the next present, so the GPU has completed that successor frame. Previously, the host waited only on the image's own previous present plus a racy mtl_last_fence() snapshot taken at acquire time, allowing two-image games that pace per-frame constants on the acquire sync alone (Xenoblade DE Switch 2, Xenoblade Chronicles 3 #3296) to overwrite live constants when nq_worker trailed, and causing three-image swapchains to over-wait on frame N-1 once replayed.

Track each slot's successor present receipt directly in win_t.next_ticket[4] (via last_pres), atomically hand both receipts to nvnWindowAcquireTexture, wait on the successor's replay publication and GPU fence (without blocking on its display/snapshot release), snapshot both receipts in nvnQueueWaitSync / nvnCommandBufferWaitSync, and drop the racy mtl_last_fence() fallback under default DM_NVN_ACQ_NEXT=1 (superseding PR #1847's own-present fence). DM_NVN_ACQ_NEXT=0 restores the legacy acquire wait for A/B isolation.

Co-authored-by: Will Varner <37232968+willv678@users.noreply.github.com>
@seedds

seedds commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @willv678 for tracking down the acquire-sync race, isolating it on Xenoblade DE Switch 2, and validating Donkey Kong Bananza alongside it. This landed on main in 1999cbd with you as co-author.

A few adjustments went in on the way to main:

  • Queue-side waits (nvnQueueWaitSync / nvnCommandBufferWaitSync): both the image's own present and the following present are now snapshotted onto the waiting queue so a separate queue waits until both have been replayed and inherits both GPU fences.
  • Per-slot successor tracking: each swapchain slot now retains its own reference to the first present that replaced it instead of scanning present sequence numbers, so changing the active texture count (3 → 2 or 2 → 3) or presenting on a parallel thread cannot drop the successor present.
  • Successor wait scope and zero-timeout polls: waiting on the successor present now waits only for replay and GPU completion (not for the successor's display-link pop or snapshot copy, which otherwise stalled t_present_snap), zero-timeout nvnSyncWait checks readiness before consuming pacing ticks, and the old mtl_last_fence() fallback is dropped when DM_NVN_ACQ_NEXT is enabled (reconciling Donkey Kong Bananza: render scale, acquire sync, and translation / GPU clock work for heavy NVN frames #1847 for three-image swapchains while keeping DM_NVN_ACQ_NEXT=0 for A/B checks).

Checked: t_nvn (574/574, including new tests for distinct-queue nvnQueueWaitSync, three-image swapchains, and 3 → 2 / 2 → 3 active-count changes; 10 of the new checks fail with DM_NVN_ACQ_NEXT=0), t_link_ticks, t_present_snap, t_present_crop, t_present_rescue, t_gfx_suspend, t_perf_order, test_docs.py, and a full Cuphead regression pass on Apple M4 Pro (59.7 fps, 0 new red lines).

Still open: retesting Xenoblade DE Switch 2, Xenoblade Chronicles 3 (#3296), Xenoblade Chronicles 2 (#3346), and Donkey Kong Bananza on main at 1999cbd.

@willv678

Copy link
Copy Markdown
Contributor Author

Thanks for landing it and for hardening it. The per-slot successor and the queue-side waits are better than what I had. And thanks @FantasyGmm for checking XC3 and XC2, glad it was the same race.

Retested Xenoblade DE Switch 2 on main at e8ac733 (M2 Pro, docked, MetalFX on): the intro run had no flat frames, same as with my branch.

Kayyluhh added a commit to Kayyluhh/nxnative that referenced this pull request Nov 27, 2026
… translation caches): closed; on main in its own form as 473c90d (mesh pipeline and run-twin cache) and 1999cbd (seedds#3372: the acquire sync waits for the successor present), with main's own store-witness growth and command-buffer growth; the GPU clock keeper (DM_GPU_KEEP) was declined upstream and goes too, as does the ubd_conflict_sh cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants